/*
 * Copyright (c) 2014 laidian. All Rights Reserved.
 * @author yaoyuming
 * @date  2020-03-26 14:09
 */
package com.yao.common.config.dynamic;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotationMetadata;

import javax.sql.DataSource;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 动态数据源注册
 * TODO 参考：https://www.jianshu.com/p/cac4759b2684
 * {@link com.baomidou.dynamic.datasource.provider.DynamicDataSourceProvider}
 * ImportBeanDefinitionRegistrar 实现数据源注册
 * EnvironmentAware 读取yml配置
 * @author yaoyuming
 * @date 2020/03/26 14:09
 */
@Slf4j
public class DynamicDataSourceRegister implements ImportBeanDefinitionRegistrar, EnvironmentAware {

    /**
      * 配置上下文，配置文件获取工具
      */
    private Environment env;


    /**
     * 数据源存储
     */
    private Map<String, DataSource> customDataSource = new LinkedHashMap<>();

    /**
     * 参数绑定工具 springboot2.0新推出
     */
    private Binder binder;


    @Override
    public void setEnvironment(Environment environment) {
        log.info("注册数据源");

        this.env = environment;

        this.binder = Binder.get(env);

    }

    @Override
    public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) {

    }
}